package com.eye.catcher.service.generate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.constants.RedisConstants;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TYwDaBaseInfo;
import com.eye.catcher.entity.TYwDaEquipmentInfo;
import com.eye.catcher.mapper.TYwDaEquipmentInfoMapper;
import com.eye.catcher.service.generate.ITYwDaEquipmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.system.user.param.UserLoginInfo;
import com.eye.catcher.system.yw.Info.param.EquipmentInfoDto;
import com.eye.catcher.system.yw.Info.param.EquipmentInfoPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-05-12
 */
@Slf4j
@Service
public class TYwDaEquipmentInfoServiceImpl extends ServiceImpl<TYwDaEquipmentInfoMapper, TYwDaEquipmentInfo> implements ITYwDaEquipmentInfoService {
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void addEquipmentInfo(TYwDaEquipmentInfo info) {
        UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+info.getCreateUserId());
        info.setCreateUserId(info.getCreateUserId());
        info.setCreateUserName(userLoginInfo.getUserDto().getUserName());
        info.setCreateLoginName(userLoginInfo.getUserDto().getLoginName());
        info.setCreateTime(LocalDateTime.now());
        info.setSource(SbCode.DATA_SOURCE_CREATE.getCode());
        this.save(info);
    }

    @Override
    public void editEquipmentInfo(TYwDaEquipmentInfo info) {
        UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+info.getUpdateUserId());
        info.setUpdateUserId(info.getCreateUserId());
        info.setUpdateUserName(userLoginInfo.getUserDto().getUserName());
        info.setUpdateLoginName(userLoginInfo.getUserDto().getLoginName());
        info.setUpdateTime(LocalDateTime.now());
        this.updateById(info);
    }

    @Override
    public void deleteByIds(String ids) {
        this.removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public Object getAllList(EquipmentInfoDto equipmentInfo) {
        QueryWrapper<TYwDaEquipmentInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,equipmentInfo);

        wrapper.orderByAsc(" SORT_NO ");
        return this.list(wrapper);
    }

    @Override
    public Object getPageList(EquipmentInfoPage equipmentInfoPage) {
        Page<TYwDaEquipmentInfo> page=new Page<>(equipmentInfoPage.getPage().getPageNum(),equipmentInfoPage.getPage().getPageSize());
        QueryWrapper<TYwDaEquipmentInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,equipmentInfoPage.getEquipmentInfo());

        wrapper.orderByAsc(" SORT_NO ");
        //list
        IPage<TYwDaEquipmentInfo> returnPage = this.page(page, wrapper);
        //总数
        returnPage.setTotal(this.count(wrapper));
        return returnPage;
    }



    private void appendWrapper(QueryWrapper<TYwDaEquipmentInfo> wrapper, EquipmentInfoDto equipmentInfo) {

        if(StringUtils.isNotEmpty(equipmentInfo.getId())){
            wrapper.eq("ID",Integer.parseInt(equipmentInfo.getId()));
        }
        if(StringUtils.isNotEmpty(equipmentInfo.getEquipmentCode())){
            wrapper.eq("EQUIPMENT_CODE",equipmentInfo.getEquipmentCode());
        }
        if(StringUtils.isNotEmpty(equipmentInfo.getEquipmentName())){
            wrapper.like("EQUIPMENT_NAME",equipmentInfo.getEquipmentName());
        }
        if(StringUtils.isNotEmpty(equipmentInfo.getCreateUserId())){
            wrapper.eq("CREATE_USER_ID",equipmentInfo.getCreateUserId());
        }
    }
}
